﻿#region <<版本注释>>
/*---------------------------------------------------------------------
* 项目名称：HttpClient
* 项目描述：
* 类 名 称：HttpUitls
* 类 描 述：
* 所在的域：DESKTOP-6CB1TV7
* 命名空间：HttpClient
* 机器名称：DESKTOP-6CB1TV7
* CLR 版本：4.0.30319.42000
* 作    者：wky
* 创建时间：2021/09/03 09:47:51
* 更新时间：2021/09/03 09:47:51
* 版 本 号:V1.0.0
***********************************************************************
* Copyright @ wky 2021. All rights reserved.
***********************************************************************
//-------------------------------------------------------------------*/
#endregion

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace HttpClient
{
    class HttpUitls
    {
        public static string Get(string Url)
        {
            //System.GC.Collect();
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
            request.Proxy = null;
            request.KeepAlive = false;
            request.Method = "GET";
            request.ContentType = "application/json; charset=UTF-8";
            request.AutomaticDecompression = DecompressionMethods.GZip;


            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            Stream myResponseStream = response.GetResponseStream();
            StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.UTF8);
            string retString = myStreamReader.ReadToEnd();


            myStreamReader.Close();
            myResponseStream.Close();


            if (response != null)
            {
                response.Close();
            }

            if (request != null)
            {
                request.Abort();
            }

            return retString;
        }



        public static string Post(string Url, string DataStr, string Referer)
        {

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
            request.Method = "POST";
            request.Referer = Referer;
            byte[] bytes = Encoding.UTF8.GetBytes(DataStr);
            //request.ContentType = "application/x-www-form-urlencoded";
            request.ContentType = "application/json; charset=UTF-8";
            request.ContentLength = bytes.Length;
            Stream myResponseStream = request.GetRequestStream();
            myResponseStream.Write(bytes, 0, bytes.Length);

            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            StreamReader myStreamReader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
            string retString = myStreamReader.ReadToEnd();

            myStreamReader.Close();
            myResponseStream.Close();

            if (response != null)
            {
                response.Close();
            }

            if (request != null)
            {
                request.Abort();
            }

            return retString;
        }
    }
}
